package com.pureut.system.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;

import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.system.domain.dto.SupplierAccessPageListDto;
import com.pureut.system.domain.dto.SupplierListPageDto;
import com.pureut.system.domain.vo.SupplierAccessVo;
import com.pureut.system.domain.vo.SupplierListVo;
import com.pureut.system.export.SupplierAccessExport;
import com.pureut.system.export.SupplierExport;
import com.pureut.system.service.ISupplierAccessService;
import com.pureut.system.util.DownTemplateUtil;
import com.pureut.system.util.ExcelUtil;
import com.pureut.system.util.FileConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * @Author: pureut
 * @Date: 2024-12-05 17:16:19
 * @Desc: 供应商准入Controller
 */
@RestController
@RequestMapping("/supplierAccess")
public class SupplierAccessController extends BaseController {

    /**
     * 供应商准入Service
     */
    @Autowired
    private ISupplierAccessService supplierAccessService;

    /**
     * 查询供应商准入列表
     * @param: params 参数列表
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierAccess-list")
    @GetMapping("/list")
    public TableDataInfo list( SupplierAccessVo supplierAccessVo) {
        startPage();
        return getDataTable(supplierAccessService.queryPage(supplierAccessVo));
    }



    /**
     * 获取供应商准入详细信息
     * @param: id 供应商准入主键
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierAccess-query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(supplierAccessService.getInfoById(id));
    }

    /**
     * 新增供应商准入
     * @param: supplierAccess 供应商准入对象
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierAccess-add")
    @PostMapping
    public AjaxResult add(@RequestBody SupplierAccessVo supplierAccessVo) {
        return toAjax(supplierAccessService.save(supplierAccessVo));
    }

    /**
     * 修改供应商准入
     * @param: supplierAccess 供应商准入对象
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierAccess-edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SupplierAccessVo supplierAccessVo) {
        return toAjax(supplierAccessService.updateById(supplierAccessVo));
    }

    /**
     * 删除供应商准入
     * @param: id 供应商准入主键
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierAccess-remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(supplierAccessService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 获取供应商准入 初始化信息
     * @param: id 样品申请主键
     * @return: Json工具类对象
     */
    @GetMapping(value = "/infoInit")
    public AjaxResult infoInit() {
        return AjaxResult.success(supplierAccessService.infoInit());
    }


    /**
     * 提交
     * @param: supplierAccess 供应商准入对象
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierAccess-edit")
    @PutMapping("/submit/{id}")
    public AjaxResult submit(@PathVariable Long id ) {
        return toAjax(supplierAccessService.submit(id));
    }

    /**
     * 审核
     * @param: supplierAccess 供应商准入对象
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierAccess-edit")
    @PutMapping("/examine")
    public AjaxResult examine(@RequestBody SupplierAccessVo supplierAccessVo) {
        return toAjax(supplierAccessService.examine(supplierAccessVo));
    }

    /**
     * 供应商准入导出
     */
    @PostMapping("/export")
    public AjaxResult export(HttpServletResponse response, SupplierAccessVo supplierAccessVo) throws IOException
    {
        List<SupplierAccessPageListDto> infoExport = supplierAccessService.export(supplierAccessVo);
        ExcelUtil<SupplierAccessPageListDto> excelUtil = new ExcelUtil<SupplierAccessPageListDto>(SupplierAccessPageListDto.class);
        excelUtil.excelWriter(response, infoExport, "导入供应商信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 供应商导入模板下载
     * @param response
     * @throws IOException
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        DownTemplateUtil.downTemplate(response, FileConstant.SUPPLIERAccess_IMPORT);
    }

    /**
     * 供应商模板导入
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "供应商模板导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SupplierAccessExport> excelUtil = new ExcelUtil<SupplierAccessExport>(SupplierAccessExport.class);
        List<SupplierAccessExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(supplierAccessService.importMouldData(coCustomer));
    }
}
